package com.qding.park.manager.service.statis.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.facade.statis.service.CarlockStatisFacade;
import com.qding.park.manager.pojo.TbCarlock;
import com.qding.park.manager.pojo.example.TbCarlockExample;
import com.qding.park.manager.pojo.example.TbCarlockExample.Criteria;
import com.qding.park.manager.service.statis.biz.CarLockStatisBiz;
import com.qding.park.manager.service.statis.mapper.TbCarlockStatisMapper;

/**
 * @author Administrator
 *锁车单统计服务
 */
@Service("carlockStatisFacade")
public class CarlockStatisFacadeImpl implements CarlockStatisFacade {
    private static final Logger _log = LoggerFactory.getLogger(CarlockStatisFacadeImpl.class); 
	/**
	 * 
	 */
	@Autowired
	private TbCarlockStatisMapper  tbCarlockMapper;
	
	@Autowired
	private  CarLockStatisBiz   carLockStatisBiz;
	
	@Override
	public ParkDataResult carLocklist(TbCarlockExample example, int jumpPage, int pageLength) throws BaseException {
		Criteria criteria = example.getOredCriteria().get(0);
		
		//以订单创建时间简
		example.orderByCreateTimeDesc();
		PageHelper.startPage(jumpPage, pageLength);
		
		List<TbCarlock> carLockList= tbCarlockMapper.listByExample(example);
		
		PageInfo page = new PageInfo(carLockList);
		List dataList = new ArrayList();
		
		int totalFee=0;
		for (TbCarlock carLock : carLockList) {
			
//			Map<String, Object> map = new HashMap<String, Object>();
//			// 订单号
//			map.put("orderNum", carLock.getId());
//			// 订单状态
//			map.put("orderStatus", carLockStatisBiz.getOrderStatusStr(carLock));
//			// 手机号
//			map.put("tel", carLock.getTel());
//			// 车牌
//			map.put("carNum", carLock.getCarNum());
//			// 车场
//			map.put("lockParkName", carLock.getParkName());
//
//			// 预定开始时间
//			map.put("startTime", carLock.getStartTime() == null ? ""
//					: DateUtils.formatDate(carLock.getStartTime(), DateUtils.DATE_FORMAT_DATETIME));
//			// 预定结束时间
//			map.put("endTime", carLock.getEndTime() == null ? ""
//					: DateUtils.formatDate(carLock.getEndTime(), DateUtils.DATE_FORMAT_DATETIME));
//			
//			// 当前状态
//			map.put("currentStatus", carLockStatisBiz.getCurrentStatus(carLock));
			dataList.add(bean2Map(carLock));
		}
		
		ParkDataResult parkDataResult = ParkDataResult.createListDate(dataList, page);
		return parkDataResult;
		
	}

	private  Map<String, Object> bean2Map(TbCarlock carLock){
		Map<String, Object> map = new HashMap<String, Object>();
		if(carLock==null){
			return map;
		}
		// 订单号
		map.put("orderNum", carLock.getId());
		// 订单状态
		map.put("orderStatus", carLockStatisBiz.getOrderStatusStr(carLock));
		// 手机号
		map.put("tel", StringUtils.isBlank(carLock.getTel())?"":carLock.getTel());
		// 车牌
		map.put("carNum", carLock.getCarNum());
		// 车场
		map.put("lockParkName", carLock.getParkName());

		// 预定开始时间
		map.put("startTime", carLock.getStartTime() == null ? ""
				: DateUtils.formatDate(carLock.getStartTime(), DateUtils.DATE_FORMAT_DATETIME));
		// 预定结束时间
		map.put("endTime", carLock.getEndTime() == null ? ""
				: DateUtils.formatDate(carLock.getEndTime(), DateUtils.DATE_FORMAT_DATETIME));
		
		// 当前状态
		map.put("currentStatus", carLockStatisBiz.getCurrentStatus(carLock));
		
		return map;
	}
	@Override
	public ParkDataResult orderInfoById(String orderId) throws BaseException {
		
		TbCarlock  tbCarlock=tbCarlockMapper.statisById(orderId);
		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		parkDataResult.putMapOnObj(bean2Map(tbCarlock));
		return parkDataResult;
	}

}
